/**
 * 发现
 * 
 * @Author : chenglm
 * @Timestamp : 2016-12-26
 */
var app = sm("do_App");
var page = sm("do_Page");
var nf = sm("do_Notification");
var rootview = ui("$");
var http = require("helper/http");
var communityUtil = require("helper/community");
var mallUrl = require("url/mall");
var do_Alayout_root = ui("do_Alayout_root");
var listdata = mm("do_ListData");
var listView_list = ui("listView_list");
listView_list.bindItems(listdata);
var pageNum = 1, totalPages = 1;
var communityId = null, categoryId = null;
//andriod返回键
page.on("back", function() {
    app.closePage();
});
/* 头部 */
var hashdata = mm("do_HashData");
var headerAout = ui("layoutHead");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);
//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
    app.closePage()
});
var header = ui("header");
header.bindData(hashdata);
hashdata.addData({
    "labelTitle" : '周边商家'
})
header.refreshData();
do_Alayout_root.add("categoryGrids", "source://view/qshop/category/firstCategoryGrid.ui", 0, 75);

page.on("loaded", function() {
	communityId = communityUtil.communityVisitId();
	refreshAllData();
});
page.on("getCategoryIdToShopList", function(data){
	categoryId = data.categoryId;
	refreshAllData();
});
function refreshAllData(){
	pageNum = 1;
	var param = {page:pageNum,communityId:communityId,categoryId:categoryId};
	http.post(mallUrl.url.shop.comShopPage, param, function(data) {
		if (data.page != null) {
			totalPages = data.page.totalPages;
		}
		listView_list.rebound();
		listdata.removeAll();
		listdata.addData([{"template":4,"adList":data.adList}]);
		listdata.addData([{"template":3,"categoryList":data.firstCategory}]);
		var datas = [];
		if (data.collectShops != null) {
			datas = datas.concat(data.collectShops);
		}
		if (data.page != null && data.page.content.length > 0) {
			datas = datas.concat(data.page.content);
		} 
		if (datas.length > 0) {
			for (var i=0; i<4; i++) {
				if (i < datas.length) {
					datas[i].template = 1;
					datas[i].haspRoductImg = datas[i].mallProductVoList.length>0?true:false;
				}
			}
			listdata.addData(datas);
		}
		else {
			var noData = [{"template":2,"labelContent":"暂时没有商家哦"}];
			listdata.addData(noData);
		}
		listView_list.refreshItems();
	});
}
function getNextPageData(){
	pageNum++;
	if (pageNum <= totalPages) {
		var param = {page:pageNum,communityId:communityId,categoryId:categoryId};
		http.post(mallUrl.url.shop.comShopPage, param, function(data) {
			totalPages = data.page.totalPages;
			listdata.addData(data.page.content);
			listView_list.rebound();
			listView_list.refreshItems();
		})
	} else {
		nf.toast("已是最后一页");
		listView_list.rebound();
	}
}

//下拉列表，刷新数据
listView_list.on("pull", function(data){
	//其中state=0：表示开始下拉headerview，；state=1：表示下拉headerview超过headerview的高度，触发一次这个事件；state=2：下拉超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){
		refreshAllData();
	}
});

//上拉列表，翻页数据
listView_list.on("push", function(data){
	//其中state=0：表示开始上推headerview，；state=1：表示上推headerview超过headerview的高度，触发一次这个事件；state=2：上推超过一定值，触发state=1事件后，松手会触发一次这个事件，数据加载完后需要调用rebound方法让header复位
	if (data.state == 2){	
		getNextPageData();
	}
});